const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: null,
    xoneData: [],
    categories: [], //bodyDate
    weightHistory: [],
    zflHistory: [],
    qztzHistory: [],
    wjyHistory: [],
    jrlHistory: [],
    dbzHistory: [],
    stsfHistory: [],
    tzlHistory: [],
    ggjHistory: [],
    nzzfHistory: [],
    jzzsHistory: [],
    jcdxHistory: [],
    znlxhHistory: [],
    bmiHistory: [],
    bodyAgeHistory: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.fetchData();
  },
  fetchData: function () {
    wx.showNavigationBarLoading();
    const phone = wx.getStorageSync('phone');
    const url = app.globalData.url;
    const that = this;
    const categories = []; //bodyDate
    const weightHistory = [];
    const zflHistory = [];
    const qztzHistory = [];
    const wjyHistory = [];
    const jrlHistory = [];
    const dbzHistory = [];
    const stsfHistory = [];
    const tzlHistory = [];
    const ggjHistory = [];
    const nzzfHistory = [];
    const jzzsHistory = [];
    const jcdxHistory = [];
    const znlxhHistory = [];
    const bmiHistory = [];
    const bodyAgeHistory = [];
    wx.request({
      url: url + '/xone/findHistory/' + phone,
      success: function (res) {
        if (res.data.success === true) {
          const xoneData = res.data.content;
          const size = xoneData.length || 0;
          if (size > 0) {
            for (var i = size - 1; i >= 0; i--) {
              const data = xoneData[i];
              categories.push(data.bodyDate);
              weightHistory.push(data.data.composition.weight.value);
              zflHistory.push(data.data.composition.fat.value);
              qztzHistory.push(data.data.composition.ffm.value);
              wjyHistory.push(data.data.composition.mineral.value);
              jrlHistory.push(data.data.composition.muscle.value);
              dbzHistory.push(data.data.composition.protein.value);
              stsfHistory.push(data.data.composition.tbw.value);
              tzlHistory.push(data.data.composition.pbf.value);
              ggjHistory.push(data.data.composition.smm.value);
              nzzfHistory.push(data.data.composition.vfi.value);
              jzzsHistory.push(data.data.composition.strong_index.value);
              jcdxHistory.push(data.data.composition.bmr.value);
              znlxhHistory.push(data.data.composition.tee.value);
              bmiHistory.push(data.data.composition.bmi.value);
              if (data.data.composition.body_age_offset) {
                bodyAgeHistory.push(data.data.composition.body_age_offset.value || 0);
              }
            }
          }
          that.setData({
            xoneData,
            categories,
            weightHistory,
            zflHistory,
            qztzHistory,
            wjyHistory,
            jrlHistory,
            dbzHistory,
            stsfHistory,
            tzlHistory,
            ggjHistory,
            nzzfHistory,
            jzzsHistory,
            jcdxHistory,
            znlxhHistory,
            bmiHistory,
            bodyAgeHistory
          });
          wx.stopPullDownRefresh();
          wx.hideNavigationBarLoading();
        } else {
          wx.stopPullDownRefresh();
          wx.hideNavigationBarLoading();
        }
      }
    })
  },

  onPullDownRefresh: function () {
    this.setData({
      xoneData: []
    });
    this.fetchData();
  },
  toDetail: function (e) {
    wx.showLoading({
      title: '请稍侯'
    });
    const index = e.currentTarget.dataset.index;
    const xoneData = this.data.xoneData;
    wx.setStorageSync('xoneData', JSON.stringify(xoneData[index]));
    wx.setStorageSync('categories', JSON.stringify(this.data.categories));
    wx.setStorageSync('weightHistory', JSON.stringify(this.data.weightHistory));
    wx.setStorageSync('zflHistory', JSON.stringify(this.data.zflHistory));
    wx.setStorageSync('qztzHistory', JSON.stringify(this.data.qztzHistory));
    wx.setStorageSync('wjyHistory', JSON.stringify(this.data.wjyHistory));
    wx.setStorageSync('jrlHistory', JSON.stringify(this.data.jrlHistory));
    wx.setStorageSync('dbzHistory', JSON.stringify(this.data.dbzHistory));
    wx.setStorageSync('stsfHistory', JSON.stringify(this.data.stsfHistory));
    wx.setStorageSync('tzlHistory', JSON.stringify(this.data.tzlHistory));
    wx.setStorageSync('ggjHistory', JSON.stringify(this.data.ggjHistory));
    wx.setStorageSync('nzzfHistory', JSON.stringify(this.data.nzzfHistory));
    wx.setStorageSync('jzzsHistory', JSON.stringify(this.data.jzzsHistory));
    wx.setStorageSync('jcdxHistory', JSON.stringify(this.data.jcdxHistory));
    wx.setStorageSync('znlxhHistory', JSON.stringify(this.data.znlxhHistory));
    wx.setStorageSync('bmiHistory', JSON.stringify(this.data.bmiHistory));
    wx.setStorageSync('bodyAgeHistory', JSON.stringify(this.data.bodyAgeHistory));
    wx.hideLoading();
    wx.navigateTo({
      url: '/pages/xoneDataDetail/xoneDataDetail'
    })
  }
})